#! /bin/bash
lib_path=$(cd `dirname $0`; pwd)


# create lib folder
if [ ! -d "${lib_path}/lib/" ];then
    mkdir ${lib_path}/lib
fi

# compile snappy dependencies
if [ -d "${lib_path}/snappy/" ]; then
    mkdir -p ${lib_path}/snappy/build
else
    echo "Please download snappy first"
    exit 1
fi

cd ${lib_path}/snappy/build
"D:\cmake-3.26.3-windows-x86_64\bin\cmake" -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -G "MinGW Makefiles" ..
"C:\msys64\mingw64\bin\mingw32-make.exe"
cp ./libsnappy.dll ../../lib/libsnappy.dll
cp ../snappy.h ../../lib/snappy.h
cp ./snappy-stubs-public.h ../../lib/snappy-stubs-public.h

# compile bitshuffle dependencies
if [ -d "${lib_path}/bitshuffle/" ]; then
    cd ${lib_path}/bitshuffle/lz4
    gcc  -shared -o liblz4.dll lz4.c -O3 -lm
    cd ${lib_path}/bitshuffle/src
    gcc -shared -o libbitshuffle.dll bitshuffle_core.c bitshuffle.c iochain.c -I../lz4 -L../lz4 -llz4 -O3 -lm
    cp ./libbitshuffle.dll  ../../lib/libbitshuffle.dll
    cp bitshuffle.h ../../lib/bitshuffle.h
    cp bitshuffle_core.h ../../lib/bitshuffle_core.h
    cd ${lib_path}/bitshuffle/lz4
    cp ./liblz4.dll ../../lib/liblz4.dll
else
    echo "Please download bitshuffle first"
    exit 1
fi

cd ${lib_path}/
g++ -shared -o ./lib/libnative.dll ./include/native.cc -L./lib -lsnappy -lbitshuffle -llz4 -I./lib -O3

# snappy4cj
echo start build snappy4cj
cjpm build -V
echo end build snappy4cj
